class BootStrap {
    // dependency injection
    def authenticateService
    def init = { servletContext ->
        if (User.findById(1) == null) {

            def authority = new Role()
            authority.authority = "ROLE_ADMIN"
            authority.description ="I'm your boss"
            authority.save()
            authority = new Role()
            authority.authority = "ROLE_TEAM"
            authority.description ="Who am I?"
            authority.save()
            def user = new User()
            user.username = "manager"
            user.userRealName = "Paul"
            user.passwd=authenticateService.encodePassword("1234")
            user.pass="1234"
            user.enabled = true
            user.email="paul@cs.ait.ac.th"
            user.emailShow=true
            user.description="BOSSSSSSSS"
            Role.findByAuthority("ROLE_ADMIN").addToPeople(user)
            user.save()
            user = new User()
            user.username = "team"
            user.userRealName = "Team Member"
            user.passwd=authenticateService.encodePassword("1234")
            user.pass="1234"
            user.enabled = true
            user.email="team@cs.ait.ac.th"
            user.emailShow=true
            user.description="TEAMMMMMM"
            Role.findByAuthority("ROLE_TEAM").addToPeople(user)
            user.save()
            user = new User()
            user.username = "team2"
            user.userRealName = "Team Member2"
            user.passwd=authenticateService.encodePassword("1234")
            user.pass="1234"
            user.enabled = true
            user.email="team2@cs.ait.ac.th"
            user.emailShow=true
            user.description="TEAMMMMMM2"
            Role.findByAuthority("ROLE_TEAM").addToPeople(user)
            user.save()
            def requestMap = new Requestmap()
            requestMap.url = "/user/create"
            requestMap.configAttribute = "ROLE_ADMIN"
            requestMap.save()
            requestMap = new Requestmap()
            requestMap.url = "/user/list"
            requestMap.configAttribute = "ROLE_ADMIN,ROLE_TEAM"
            requestMap.save()
            requestMap = new Requestmap()
            requestMap.url = "/user/show/**"
            requestMap.configAttribute = "ROLE_ADMIN,ROLE_TEAM"
            requestMap.save()
            requestMap.url = "/user/edit/**"
            requestMap.configAttribute = "ROLE_ADMIN,ROLE_TEAM"
            requestMap.save()
            requestMap.url = "/user/delete/**"
            requestMap.configAttribute = "ROLE_ADMIN"
            requestMap.save()
            requestMap = new Requestmap()
            requestMap.url = "/requestmap/**"
            requestMap.configAttribute = "ROLE_ADMIN"
            requestMap.save()
            requestMap = new Requestmap()
            requestMap.url = "/task/create/**"
            requestMap.configAttribute = "ROLE_ADMIN"
            requestMap.save()
            requestMap = new Requestmap()
            requestMap.url = "/task/edit/**"
            requestMap.configAttribute = "ROLE_ADMIN"
            requestMap.save()
            requestMap = new Requestmap()
            requestMap.url = "/task/delete/**"
            requestMap.configAttribute = "ROLE_ADMIN"
            requestMap.save()
            requestMap = new Requestmap()
            requestMap.url = "/project/create/**"
            requestMap.configAttribute = "ROLE_ADMIN"
            requestMap.save()
            requestMap = new Requestmap()
            requestMap.url = "/project/edit/**"
            requestMap.configAttribute = "ROLE_ADMIN"
            requestMap.save()
            requestMap = new Requestmap()
            requestMap.url = "/project/delete/**"
            requestMap.configAttribute = "ROLE_ADMIN"
            requestMap.save()
            requestMap = new Requestmap()
            requestMap.url = "/subTask/create/**"
            requestMap.configAttribute = "ROLE_ADMIN"
            requestMap.save()
            requestMap = new Requestmap()
            requestMap.url = "/subTask/edit/**"
            requestMap.configAttribute = "ROLE_ADMIN"
            requestMap.save()
            requestMap = new Requestmap()
            requestMap.url = "/subTask/delete/**"
            requestMap.configAttribute = "ROLE_ADMIN"
            requestMap.save()
            requestMap = new Requestmap()
            requestMap.url = "/projectTeam/**"
            requestMap.configAttribute = "ROLE_ADMIN"
            requestMap.save()
		
            def project = new Project()
            project.name = "BurnDownChart"
            project.description = "Display Burn Down Chart"
            project.startDate = new Date(2010, 7, 25)
            project.endDate = new Date(2010, 10, 24)
            project.unitOfWork = 10
            project.unitPerPhase = 10
            project.save()
		
            def WAE = new Project(name:"WAE", description: "An Agile Ruby Project.", startDate: new Date() - 30, endDate: new Date() + 30, unitPerPhase: 10)
            WAE.save()
		
            def tasks = [
                new Task(project: project, name:"PSP0", description: "SD Real Number", startDate: new Date(2010, 8, 1), endPlanDate: new Date(2010, 8, 6), endDate: new Date(2010, 8, 7), hours: 5),
                new Task(project: project, name:"PSP0.1", description: "LOC Counter", startDate: new Date(2010, 8, 1), endPlanDate: new Date(2010, 8, 6), endDate: new Date(2010, 8, 7), hours: 5),
                new Task(project: project, name:"PSP1", description: "Linear Regression", startDate: new Date(2010, 9, 1), endPlanDate: new Date(2010, 9, 6), endDate: new Date(2010, 9, 7), hours: 5),
                new Task(project: WAE,  name:"PS1", description: "Basics", startDate: new Date(2010, 8, 1), endPlanDate: new Date(2010, 8, 6), endDate: new Date(2010, 8, 7), hours: 5),
            ]
            tasks*.save(flush:true)
		
            def subTasks = [
                new SubTask(phaseName: "Plainning", estimatedTime: 2, finalTime: 0, unitForPhase: 5, task: Task.findByName("PSP0"), user: User.findByUsername("team")),
                new SubTask(phaseName: "Design", estimatedTime: 2, finalTime: 0,unitForPhase: 5, task: Task.findByName("PSP0"), user: User.findByUsername("team")),
                new SubTask(phaseName: "Code", estimatedTime: 2, finalTime: 0,unitForPhase: 5, task: Task.findByName("PSP0"), user: User.findByUsername("team2")),
                new SubTask(phaseName: "Test", estimatedTime: 2, finalTime: 0,unitForPhase: 5, task: Task.findByName("PSP0"), user: User.findByUsername("team")),
                new SubTask(phaseName: "Postmortem", estimatedTime: 2, finalTime: 0,unitForPhase: 5, task: Task.findByName("PSP0"), user: User.findByUsername("team2")),
                new SubTask(phaseName: "Planning", estimatedTime: 2, finalTime: 0,unitForPhase: 5, task: Task.findByName("PSP0.1"), user: User.findByUsername("team")),
                new SubTask(phaseName: "Design", estimatedTime: 2, finalTime: 0,unitForPhase: 5, task: Task.findByName("PSP0.1"), user: User.findByUsername("team2")),
                new SubTask(phaseName: "Code", estimatedTime: 2, finalTime: 0,unitForPhase: 5, task: Task.findByName("PSP0.1"), user: User.findByUsername("team")),
                new SubTask(phaseName: "Test", estimatedTime: 2, finalTime: 0,unitForPhase: 5, task: Task.findByName("PSP0.1"), user: User.findByUsername("team2")),
                new SubTask(phaseName: "Postmortem", estimatedTime: 2, finalTime: 0,unitForPhase: 5, task: Task.findByName("PSP0.1"), user: User.findByUsername("team2")),
            ]
            subTasks*.save(flush:true)
		
            def projectTeam = new ProjectTeam()
            projectTeam.project = Project.get(1)
            projectTeam.user = User.get(1)
            projectTeam.save()
            projectTeam = new ProjectTeam()
            projectTeam.project = Project.get(1)
            projectTeam.user = User.get(2)
            projectTeam.save()
            projectTeam = new ProjectTeam()
            projectTeam.project = Project.get(1)
            projectTeam.user = User.get(3)
            projectTeam.save()
		
		
            def pspData = [
                new PspData(phaseName: "Planning", startDate: new Date(), actualTimeInMin: 8, interuptTimeInMin: 2, task: Task.findById(1), user: User.findByUsername("team")),
                new PspData(phaseName: "Design", startDate: new Date(), actualTimeInMin: 10, interuptTimeInMin: 2, task: Task.findById(1), user: User.findByUsername("team")),
                new PspData(phaseName: "Code", startDate: new Date(), actualTimeInMin: 10, interuptTimeInMin: 2, task: Task.findById(1), user: User.findByUsername("team")),
                new PspData(phaseName: "Postmortem", startDate: new Date(), actualTimeInMin: 10, interuptTimeInMin: 2, task: Task.findById(6), user: User.findByUsername("team2"))
            ]
            pspData*.save(flush:true)
        }
    }
    def destroy = {
    }
} 
